package com.sky.handler;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;
import java.util.HashMap;

/**
 * @projectName: netty-cloud
 * @package: com.sky.handler
 * @className: MySessionInformationExceptionStrategy
 * @author: zhl
 * @description: 多用户登录访问并发处理
 * @date: 2024/4/25 15:24
 * @version: 1.0
 */
public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        HashMap<String,Object> hashMap = new HashMap<>(16);
        hashMap.put("code",400);
        hashMap.put("msg","failure");
        hashMap.put("data","登录已过期，请重新登录(该账号已在其他设备登录)");
        String json = JSON.toJSONString(hashMap);

        HttpServletResponse response = event.getResponse();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(json);
    }
}
